package edu.must.uh.servlet;

import java.util.List;
import java.util.Map;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import edu.must.uh.bean.DeployQueryResultBean;
import edu.must.uh.impl.BaseUserExImpl;
import edu.must.uh.impl.DepartmentImpl;
import edu.must.uh.util.R;
import edu.must.uh.util.ServletUtils;
import edu.must.uh.util.Tools;
import edu.must.uh.util.ToolsOfString;

@WebServlet("/DepartmentServlet")
public class DepartmentServlet extends BaseServlet {
    private static final long serialVersionUID = 12197442526341123L;
    private static Logger logger = Logger.getLogger(DepartmentServlet.class);  
    
    
    public Object getList(HttpServletRequest request, HttpServletResponse response) throws Exception {      
     
    	DepartmentImpl departmentImpl = new DepartmentImpl();
    	departmentImpl._PageBean = super.pageBean;
        Map<String, Object> searchcondition = ServletUtils.getsearchcondition(request);
        DeployQueryResultBean resList = departmentImpl.getLimitlist(searchcondition, " ORDER BY SortCode ");
                 
        return R.succ(resList.getResultList(),resList.getTotal());         
    }
    
    public Object getOneData(HttpServletRequest request, HttpServletResponse response) throws Exception {      
        
    	DepartmentImpl departmentImpl = new DepartmentImpl();    	
    	String keyValue =ToolsOfString.NulltoEmpty(request.getParameter("DepartmentId")); 
    	String sqlString = "SELECT  DepartmentId, ParentId,FullName,ManagerId,RealName,SortCode FROM v_base_department Where DELETEMARK= 0  ";
    	sqlString = sqlString + " AND DepartmentId = '"+keyValue+"'";
        Map<String,Object>  map = departmentImpl.getMap(sqlString);                 
        return R.succ(map);         
    }
    
    
    public Object getDepartmentSelect(HttpServletRequest request, HttpServletResponse response) throws Exception {      
        
    	DepartmentImpl departmentImpl = new DepartmentImpl();   	
    	List<Map<String, Object>> resList = departmentImpl.getDepartmentSelect();                 
        return R.succ(resList);         
    }
    
    public Object delete(HttpServletRequest request, HttpServletResponse response) throws Exception {      
        
    	DepartmentImpl departmentImpl = new DepartmentImpl();
        String keyValue =ToolsOfString.NulltoEmpty(request.getParameter("DepartmentId")); 
        Integer resInteger = departmentImpl.delete(keyValue);
        return R.succ(resInteger);         
    }
    
  public Object save(HttpServletRequest request, HttpServletResponse response) throws Exception {  
      
		DepartmentImpl departmentImpl = new DepartmentImpl();
		String sql = "";
		Map<String, Object> searchcondition = ServletUtils.getsearchcondition(request);
	    String DepartmentId =ToolsOfString.NulltoEmpty(searchcondition.get("DepartmentId"));
        String ParentId =ToolsOfString.NulltoEmpty(searchcondition.get("ParentId"));  
        String FullName =ToolsOfString.NulltoEmpty(searchcondition.get("FullName"));
        FullName =  java.net.URLDecoder.decode(FullName, "UTF-8");
        String ManagerId =ToolsOfString.NulltoEmpty(searchcondition.get("ManagerId"));
        String SortCode =ToolsOfString.NulltoEmpty(searchcondition.get("SortCode"));
        if(Tools.notEmpty(DepartmentId)) {
        	sql = "UPDATE base_department  SET FullName = '"+FullName+"',ManagerId = '"+ManagerId+"',SortCode = '" +SortCode+"'";
        	sql = sql + " WHERE DepartmentId ='"+DepartmentId+"'"; 
        }else {
        	DepartmentId = Tools.getUUID32();
        	sql = "INSERT INTO base_department(DepartmentId,FullName,ManagerId,SortCode)VALUES";
        	sql = sql + " ('"+DepartmentId+"','"+FullName+"','"+ManagerId+"','"+SortCode+"')"; 
        }
        Integer resInteger = departmentImpl.save(sql);
        if(resInteger == 1) {
        	   return R.succ(); 
        }
        return R.fail();
       
    }
    
    
    
   
}
